package org.brownflat.watergate.domain;

import org.hibernate.annotations.Cascade;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
@Entity
@Table(name="wgate_review")
public class Review extends BaseEntity {

    @ManyToOne
    @JoinColumn(name="creator")
    private User creator;

    @OneToMany
    @Cascade( { org.hibernate.annotations.CascadeType.ALL } )
    @JoinColumn(name="review")
    private List<ReviewContribution> reviewContributions = new ArrayList<ReviewContribution>();

    @OneToMany
    @JoinTable(
            name = "wgate_review_commit",
            joinColumns = @JoinColumn(name = "review_id"),
            inverseJoinColumns = @JoinColumn(name = "commit_id")
    )
    private List<Commit> commits = new ArrayList<Commit>();

    @OneToMany
    @JoinTable(
            name = "wgate_review_comment",
            joinColumns = @JoinColumn(name = "review_id"),
            inverseJoinColumns = @JoinColumn(name = "comment_id")
    )
    private List<Comment> comments = new ArrayList<Comment>();

    @Column
    private String title;

    @ManyToOne
    @JoinColumn(name="review_state")
    private ReviewState reviewState;

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public List<ReviewContribution> getReviewContributions() {
        return reviewContributions;
    }

    public void setReviewContributions(List<ReviewContribution> reviewContributions) {
        this.reviewContributions = reviewContributions;
    }

    public void addReviewContribution(ReviewContribution reviewContribution) {
        this.reviewContributions.add(reviewContribution);
    }

    public List<Commit> getCommits() {
        return commits;
    }

    public void setCommits(List<Commit> commits) {
        this.commits = commits;
    }

    public void addCommit(Commit commit) {
        this.commits.add(commit);
    }

    public List<Comment> getComments() {
        return comments;
    }

    public void addComment(Comment comment) {
        this.comments.add(comment);
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public ReviewState getReviewState() {
        return reviewState;
    }

    public void setReviewState(ReviewState reviewState) {
        this.reviewState = reviewState;
    }

    public boolean hasCommit(Commit commit) {
        return commits.contains(commit);
    }

    public int countContributors() {
        return reviewContributions.size();
    }

    public boolean hasBlah() {
        for(ReviewContribution reviewContribution : reviewContributions) {
            if(reviewContribution.getReviewContributionState() == null) {
                return true;
            }
        }
        return false;
    }
}
